package dao;

import hibernateUtils.HibernateUtil;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;

import beans.ComisionBean;

public class HibernateComisionDAO {
	private static HibernateComisionDAO instancia;
	private static SessionFactory sf;

	public static HibernateComisionDAO getInstancia() {
		if (instancia == null) {
			sf = HibernateUtil.getSessionFactory();
			instancia = new HibernateComisionDAO();
		}
		return instancia;
	}

	public ComisionBean buscarComision(ComisionBean ab) {
		Session session = sf.openSession();
		ComisionBean comision = (ComisionBean) session.get(ComisionBean.class,
				ab.getIdComision());
		session.close();
		return comision;
	}

	public List<ComisionBean> getComisions(int idSucursal) {
		Session session = sf.openSession();
		List<ComisionBean> comisions = session
				.createQuery("from ComisionBean a where a.idComision=?")
				.setInteger(0, idSucursal).list();
		return comisions;
	}

	public void persist(ComisionBean ab) {
		Session session=sf.openSession();
		session.beginTransaction();
		session.persist(ab);
		session.flush();
		session.beginTransaction().commit();
		session.close();
	}

	public ComisionBean buscarUltimaComision() {
		Session session=sf.openSession();
		List<ComisionBean> comisiones=(List<ComisionBean>) session.createQuery("from ComisionBean").list();
		session.close();
		return comisiones.get(comisiones.size()-1);
	}
}
